package com.yf.controller.rongcloud;

import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUser;
import com.yf.entity.WxUser;
import com.yf.response.R;
import com.yf.service.WxWebService;
import com.yf.service.rongcloud.UsersService;
import com.yf.vo.rongcloud.TokenVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: Administrator
 * Date: 2018-10-22
 * Time: 11:30
 * Description:
 */
@RestController
@RequestMapping("/api/rongCloud")
public class ApiRongCloudController {
    @Autowired
    private UsersService usersService;


    @Autowired
    private WxWebService wxWebService;



    /**
     * 微信公众号获取code
     * @return
     */
    @RequestMapping("/wx/code")
    @IgnoreAuth
    public void wxCode(String backUrl, HttpServletResponse response) throws IOException {
        String codeUrl = wxWebService.getCodeUrl(backUrl);
        //带上code之后会跳转回去
        response.sendRedirect(codeUrl);
    }

    /**
     * 微信公众号登录
     * @param code
     * @param
     * @return
     */
    @RequestMapping("/wx/web/login")
    @IgnoreAuth
    public R wxWebLogin(String code){
        WxUser wxUser = wxWebService.getWxUser(code);
        TokenVO token = usersService.registerUser(wxUser);
        return R.ok("登录成功", token).putExtra("wxUser",wxUser);
    }


    /**
     * 融云加群操作
     * @param groupId
     * @param
     * @return
     */
    @RequestMapping("/wx/group/join")
    public R joinGroup(Long groupId,Long userId){
        usersService.joinGroup(groupId,userId);
        return R.ok();
    }

}
